import pygame
import random
import queue

# 子弹类
class Bullet:
    def __init__(self, x, y, bullet_image):
        self.x = x
        self.y = y
        self.speed = 5
        self.bullet_image = bullet_image

    def move(self):
        self.y -= self.speed

    def draw(self, screen):
        screen.blit(self.bullet_image, (self.x, self.y))

def run(coord_queue):
    # 敌机列表
    # 初始化 Pygame
    pygame.init()

    # 设置屏幕大小
    screen_width = 1000
    screen_height = 800
    screen = pygame.display.set_mode((screen_width, screen_height))

    # 设置标题
    pygame.display.set_caption("飞机大战")

    # 加载图像
    player_image = pygame.image.load('assets/player.png')
    enemy_image = pygame.image.load('assets/enemy.jpeg')
    bullet_image = pygame.image.load('assets/bullet.png')  # 加载子弹图像
    # 缩小图像
    player_image = pygame.transform.scale(player_image, (50, 50))  # 将玩家图像缩放到 50x50 像素
    enemy_image = pygame.transform.scale(enemy_image, (50, 50)) 
    bullet_image = pygame.transform.scale(bullet_image, (10, 20))  # 将子弹图像缩放到 10x20 像素
    enemies = []

    # 子弹列表
    bullets = []

    # 分数
    score = 0
    font = pygame.font.Font(None, 36)
    # 游戏循环标志
    running = True
    # 设置玩家初始位置
    player_x = screen_width // 2
    player_y = screen_height - 100
    player_speed = 1
    # 游戏主循环
    while running:
        # 处理事件
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:  # 按空格键发射子弹
                    bullet_x = player_x + player_image.get_width() // 2 - bullet_image.get_width() // 2
                    bullet_y = player_y
                    bullets.append(Bullet(bullet_x, bullet_y, bullet_image))

        # 获取键盘输入
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT] and player_x > 0:
            player_x -= player_speed
        if keys[pygame.K_RIGHT] and player_x < screen_width - player_image.get_width():
            player_x += player_speed
        if keys[pygame.K_UP] and player_y > 0:
            player_y -= player_speed
        if keys[pygame.K_DOWN] and player_y < screen_height - player_image.get_height():
            player_y += player_speed
        # 非阻塞地从队列中获取数据
        try:
            data = coord_queue.get_nowait()
            if isinstance(data, tuple) and len(data) == 2:  # 如果数据是坐标
                player_x, player_y = data
            elif isinstance(data, int) and data == 1:  # 如果数据是整数1
                bullet_x = player_x + player_image.get_width() // 2 - bullet_image.get_width() // 2
                bullet_y = player_y
                bullets.append(Bullet(bullet_x, bullet_y, bullet_image))
        except queue.Empty:
            pass
        # 随机生成敌机
        if random.randint(1, 300) == 1:
            enemy_x = random.randint(0, screen_width - enemy_image.get_width())
            enemy_y = -enemy_image.get_height()
            enemies.append([enemy_x, enemy_y])

        # 移动敌机
        for enemy in enemies:
            enemy[1] += 0.1
            if enemy[1] > screen_height:
                enemies.remove(enemy)

        # 移动子弹
        for bullet in bullets:
            bullet.move()
            if bullet.y < 0:
                bullets.remove(bullet)

        # 检测子弹与敌人的碰撞
        for bullet in bullets[:]:
            bullet_rect = pygame.Rect(bullet.x, bullet.y, bullet_image.get_width(), bullet_image.get_height())
            for enemy in enemies[:]:
                enemy_rect = pygame.Rect(enemy[0], enemy[1], enemy_image.get_width(), enemy_image.get_height())
                if bullet_rect.colliderect(enemy_rect):
                    bullets.remove(bullet)
                    enemies.remove(enemy)
                    score += 10  # 击中敌人后增加分数

        # 检测玩家与敌人的碰撞
        player_rect = pygame.Rect(player_x, player_y, player_image.get_width(), player_image.get_height())
        for enemy in enemies:
            enemy_rect = pygame.Rect(enemy[0], enemy[1], enemy_image.get_width(), enemy_image.get_height())
            if player_rect.colliderect(enemy_rect):
                running = False

        # 绘制背景
        screen.fill((255, 192, 203)) 

        # 绘制玩家
        screen.blit(player_image, (player_x, player_y))

        # 绘制敌机
        for enemy in enemies:
            screen.blit(enemy_image, (enemy[0], enemy[1]))

        # 绘制子弹
        for bullet in bullets:
            bullet.draw(screen)

        # 绘制分数
        score_text = font.render(f'Score: {score}', True, (255, 255, 255))
        screen.blit(score_text, (10, 10))

        # 更新屏幕
        pygame.display.flip()

    # 退出 Pygame
    pygame.quit()

if __name__ == "__main__":
    coord_queue = queue.Queue()
    run(coord_queue)